package cn.ante.product.service.impl;

import cn.ante.basic.constant.BaseContants;
import cn.ante.basic.utils.BaiduAuditUtils;
import cn.ante.pet.domain.Pet;
import cn.ante.pet.domain.PetDetail;
import cn.ante.pet.domain.PetOnlineAuditLog;
import cn.ante.pet.mapper.PetDetailMapper;
import cn.ante.product.domain.Product;
import cn.ante.product.domain.ProductDetail;
import cn.ante.product.domain.ProductOnlineAuditLog;
import cn.ante.product.mapper.ProductDetailMapper;
import cn.ante.product.mapper.ProductMapper;
import cn.ante.product.mapper.ProductOnlineAuditLogMapper;
import cn.ante.product.service.IProductService;
import cn.ante.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Nai
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void add(Product product) {
        productMapper.add(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }

    @Override
    public void offSale(Long[] ids) {
        productMapper.offSale(ids);
    }

    @Override
    public void onSale(Long[] ids) {
        ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
        // 上架的时候需要一一审核，所以需要对此进行处理
        for (Long id : ids){
            Product product = productMapper.loadById(id);

            // 记录审核失败日志
            StringBuffer stringBuffer = new StringBuffer();

            if (!BaiduAuditUtils.TextCensor(product.getName())){
                stringBuffer.append("名称审核失败，名称不合法" + product.getName());
            }
            for (String filePath:product.getResources().split(",")){
                if (!BaiduAuditUtils.ImgCensor(filePath.getBytes())){
                    stringBuffer.append("图片审核失败，图片不合法" + BaseContants.ImageAudit.CALL_ADDRESS + filePath);
                }else{

                }
            }
            if (stringBuffer.length() > 0){
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLog.setNote(stringBuffer.toString());
                productOnlineAuditLogMapper.add(productOnlineAuditLog);
            }else{
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLog.setNote("审核成功");
                productOnlineAuditLogMapper.add(productOnlineAuditLog);
            }
            productMapper.onSale(id);
        }
    }

    @Override
    public Product loadByIdDetail(Long id) {
        return productMapper.loadByIdDetail(id);
    }
}
